<?php
/*
 * Created on 2010-5-31
 * 
 * Filename: fns.php
 *
 * Author: szy
 * 
 * TODO: TODO
 *
 */

  function filled_out($form_vars){
  	
  	//test that each variable has a value
  	foreach($form_vars as $key => $value){
  	
  	  if(!isset($key) || ($value == '')){
  	  
  	    
  	    return false;	
  	  	
  	  }  //end if	
  		
  	}  //end foreach
  	
  	return true;
  	
  }  //end function filled_out
  
  function valid_email($address){
  
    //check an email address is possibly valid
    if(ereg('^[a-zA-Z0-9\._\-]+@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]+$', $address)) return true;
    else 
      return false;
  	
  }  //end function valid_email


//register new person with db
//return true or error message
function register($email,$passwd){
  
    //connect to db
    $conn = db_connect();

    //check if username is unique
    $result = $conn->query("select * from users where user_name='$email'");
    if(!$result) throw new Exception('Could not execute query');
    if($result->num_rows>0) throw new Exception('That email is taken. Go back and choose another one.');

    //if ok, put in db
    $result = $conn->query("INSERT INTO bbox.users(user_name, user_passwd, user_group, capacity) VALUES('$email', '$passwd', 'normal', 1024);");

    if(!$result) throw new Exception('Could not register you in database. Please try again later.');
  	
  	return true;
  	
}  //end function register

  //check username and password with db
  //if yes, return true
  //else throw exception
  function login($email, $passwd){
  
    //connect to db
    $conn = db_connect();
    
    //check if email is unique
    $result = $conn->query("select user_name from users where user_name = '$email' and user_passwd = '$passwd'");    
  	
  	if(!$result) throw new Exception('Could not log you in.');
  	
  	if($result->num_rows>0) return true;
  	else throw new Exception('Could not log you in.');
  	
  }  //end function login

  //see if somebody is logged in and nofity them if not
  function check_valid_user(){
  
    if(isset($_SESSION['valid_user'])){
    
      echo 'Logged in as '.stripslashes($_SESSION['valid_user']).'.';
      echo '<br />';	
    	
    }  //end if	
    else{
    	
    	//they are not logged in
    	echo 'You are not logged in.<br />';
    	exit;
    	
    }  //end else
  	
  	
  }  //end function check_valid_user

  ///check whether the user exists or not
  function user_check($user_name){
  
    $conn = db_connect();
    
    $result	= $conn->query("select * from users where user_name = '$user_name'");
  	
  	if(!$result) throw new Exception('Could not execute query.');
  	if($result->num_rows<1) throw new Exception('There is not the same account in the system.');
  	
  }  //end function user_check

  //set password for username to a random value
  //return the new password or false on failure
  function reset_password($user_name){
  	
  	//check whether the user exists or not
  	user_check($user_name);
  	
  	
  	//get a random dictionary word b/w 6 and 13 chars in length
  	$new_password = get_random_word(6, 13);
  	
  	if($new_password === false) throw new Exception('Could not generate new password.');
  
    /*	
  	//add a number between 0 and 999 to it
  	//to make it a slightly better password
  	srand((double)microtime()*1000000);
  	$rand_number = rand(0, 999);
  	
  	$new_passwd .= $rand_number;
  	*/
  	
  	//set user's password to this in database or return false
  	$conn = db_connect();
  	
  	$result = $conn->query("update users set user_passwd = '$new_password' where user_name= '$user_name'");

  	
  	//not change
  	if(!$result) throw new Exception('Could not change password.');
  	//changed successfully
  	else return $new_password;
  
  }  //end function reset_password
  
  //grab a random word from dictionary between the two legths
  //and return it
  function get_random_word($min_length, $max_length){
  
    
    //generate a random word
    $word = '';
    
    /*
    //remember to change this path to suit your system
    $dictionary = '/usr/dict/words';  //the ipell dictionary
    
    $fp = @ fopen($dictionary, 'r');
    
    //go to a random location in dictionary
    srand((double)microtime()*1000000);
    $rand_location = rand(0, $size);
    fseek($fp, $rand_location);
    
    //get the next whole word of the right length the file
    while(strlen($word)<$min_length || strlen($word)>$max_length  ||strstr($word, "'")){
    
      //if at end, go to start
      if(feof($fp))  fseek($fp, 0);
      
      //skip first word as it could be partial
      $word = fgets($fp, 80);
      
      //the potential password
      $word = fgets($fp, 80);
    	
    }  //end while
    
    //trim the trailing  \n from fgets
    $word = trim($word);
    
    */
    while(strlen($word)<$min_length || strlen($word)>$max_length){
    
      	$word = $word.rand(0,9);
    	
    }  //end while
    
    
    return $word;
    	
  	
  }  //end function get_random_word
  
  //notify the user that password has been changed
  function notify_password($user_name, $password){

/*  
    conn = db_connect();
    $result = $conn->query("select user_name from users where user_name = '$user_name'");	
  	
  	if(!$result){
  	
  	  throw new Exception('Could not find email address.');	
  	
  	}  //end if
  	else if($result->num_rows == 0){
  	
  	  //username not in db
  	  throw new Exception('Could not find email address.');	
  		
  	}  //end else if
  	else{
  	
  	  $row = $result->fetch_object();
  	  $email = $row->user_name;
  	  $from = "From: songzy@hsit.com.cn \r\n";
  	  $mesg = "Your test enviroment manager password has been changed to $password \r\n"
  	          ."Please change it next time you log in.\r\n";	
  		
  		if(mail($email, 'BBox login information', $mesg, $from)) return true;
  		else throw new Exception('Could not send email.');
  		
  	}  //end else
  */
      $email = $user_name;
      $from = "From: songzy@hsit.com.cn \r\n";
      $mesg = "Your Bbox password has been changed to $password \r\n"
          ."Please change it next time you log in.\r\n";	

      if(mail($email, 'BBox login information', $mesg, $from)) return true;
      else throw new Exception('Could not send email.');
  	
  }  //end function notify_password


?>
